package com.wanhong.springboot.ssm.framework.filter;

import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author w_yu
 * @date 2020/11/26 14:53
 * @description: 解决js跨域问题
 **/
@WebFilter
@Configuration
public class CORSFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Credentials","true");
        response.setHeader("Access-Control-Allow-Methods","*");
        response.setHeader("Access-Control-Max-Age","60");
        response.setHeader("Access-Control-Allow-Headers","Origin, X-Requested-With, Content-Type, Accept, AccessToken");
        response.setHeader("Access-Control-Expose-Headers","Origin, X-Requested-With, Content-Type, Accept, AccessToken");
        filterChain.doFilter(servletRequest,servletResponse);
    }
}
